package com.mybatis.demo.mapper;

import com.mybatis.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    // 1. 数据库重命名
    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info")
    public List<UserInfo> selectAll();



    // 2.使用注解
    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info")
    @Results(id = "BaseMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })
    public List<UserInfo> selectAll2();

    @ResultMap("BaseMap")
    @Select("select * from user_info where id = 2")
    public List<UserInfo> selectById();

    @Select("select * from user_info where id = #{id}")
    public List<UserInfo> selectById2(Integer id);

    @Select("select * from user_info where id = #{userId}")
    public List<UserInfo> selectById3(@Param("userId") Integer id);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age, gender, phone) values (#{username},#{password},#{age},#{gender},#{phone})")
    public Integer insert(UserInfo userInfo);

    @Delete("delete from user_info where id =#{id}")
    public Integer delete(Integer id);

    @Update("update user_info set username = #{username} where id = #{id}")
    public Integer update(String username,Integer id);


    // #{} 和 ${} 的区别
    // ${} 是直接替换的，存在sql注入问题

    @Select("select * from user_info where username = #{username}")
    public List<UserInfo> selectByName(String username);

    @Select("select * from user_info where username = '${username}'")
    public List<UserInfo> selectByName2(String username);


}
